Public Class frmCampaignerPasswordDialog
    Inherits System.Windows.Forms.Form

    Private ptrGameData As cmplib01.CampaignerGameData

    Private m_ActionCancelled As Boolean

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents pnlTopBorder As System.Windows.Forms.Panel
    Friend WithEvents picCampaignerIcon As System.Windows.Forms.PictureBox
    Friend WithEvents txtTitleBox As System.Windows.Forms.TextBox
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents btnLogon As System.Windows.Forms.Button
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents lstPlayerSelect As System.Windows.Forms.ListBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtPassword As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmCampaignerPasswordDialog))
        Me.pnlTopBorder = New System.Windows.Forms.Panel()
        Me.picCampaignerIcon = New System.Windows.Forms.PictureBox()
        Me.txtTitleBox = New System.Windows.Forms.TextBox()
        Me.btnCancel = New System.Windows.Forms.Button()
        Me.btnLogon = New System.Windows.Forms.Button()
        Me.Panel1 = New System.Windows.Forms.Panel()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.txtPassword = New System.Windows.Forms.TextBox()
        Me.lstPlayerSelect = New System.Windows.Forms.ListBox()
        Me.pnlTopBorder.SuspendLayout()
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'pnlTopBorder
        '
        Me.pnlTopBorder.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.pnlTopBorder.BackColor = System.Drawing.Color.SteelBlue
        Me.pnlTopBorder.Controls.AddRange(New System.Windows.Forms.Control() {Me.picCampaignerIcon, Me.txtTitleBox})
        Me.pnlTopBorder.Name = "pnlTopBorder"
        Me.pnlTopBorder.Size = New System.Drawing.Size(416, 40)
        Me.pnlTopBorder.TabIndex = 9
        '
        'picCampaignerIcon
        '
        Me.picCampaignerIcon.Image = CType(resources.GetObject("picCampaignerIcon.Image"), System.Drawing.Bitmap)
        Me.picCampaignerIcon.Location = New System.Drawing.Point(9, 11)
        Me.picCampaignerIcon.Name = "picCampaignerIcon"
        Me.picCampaignerIcon.Size = New System.Drawing.Size(16, 16)
        Me.picCampaignerIcon.TabIndex = 9
        Me.picCampaignerIcon.TabStop = False
        '
        'txtTitleBox
        '
        Me.txtTitleBox.BackColor = System.Drawing.Color.SteelBlue
        Me.txtTitleBox.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.txtTitleBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtTitleBox.ForeColor = System.Drawing.Color.WhiteSmoke
        Me.txtTitleBox.Location = New System.Drawing.Point(32, 8)
        Me.txtTitleBox.Multiline = True
        Me.txtTitleBox.Name = "txtTitleBox"
        Me.txtTitleBox.ReadOnly = True
        Me.txtTitleBox.Size = New System.Drawing.Size(312, 26)
        Me.txtTitleBox.TabIndex = 8
        Me.txtTitleBox.Text = "Campaigner [Log On]"
        '
        'btnCancel
        '
        Me.btnCancel.BackColor = System.Drawing.Color.LightSkyBlue
        Me.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnCancel.ForeColor = System.Drawing.Color.Black
        Me.btnCancel.Location = New System.Drawing.Point(328, 232)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.TabIndex = 11
        Me.btnCancel.Text = "Cancel"
        '
        'btnLogon
        '
        Me.btnLogon.BackColor = System.Drawing.Color.LightSkyBlue
        Me.btnLogon.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnLogon.ForeColor = System.Drawing.Color.Black
        Me.btnLogon.Location = New System.Drawing.Point(248, 232)
        Me.btnLogon.Name = "btnLogon"
        Me.btnLogon.TabIndex = 10
        Me.btnLogon.Text = "Logon"
        '
        'Panel1
        '
        Me.Panel1.BackColor = System.Drawing.Color.LightSkyBlue
        Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label1, Me.txtPassword, Me.lstPlayerSelect})
        Me.Panel1.Location = New System.Drawing.Point(4, 48)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(402, 176)
        Me.Panel1.TabIndex = 12
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.ForeColor = System.Drawing.Color.Black
        Me.Label2.Location = New System.Drawing.Point(16, 125)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(224, 16)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Enter Password"
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.ForeColor = System.Drawing.Color.Black
        Me.Label1.Location = New System.Drawing.Point(16, 5)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(120, 19)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Select Player"
        '
        'txtPassword
        '
        Me.txtPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtPassword.Location = New System.Drawing.Point(8, 144)
        Me.txtPassword.Name = "txtPassword"
        Me.txtPassword.PasswordChar = Microsoft.VisualBasic.ChrW(42)
        Me.txtPassword.Size = New System.Drawing.Size(384, 20)
        Me.txtPassword.TabIndex = 1
        Me.txtPassword.Text = ""
        '
        'lstPlayerSelect
        '
        Me.lstPlayerSelect.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lstPlayerSelect.Location = New System.Drawing.Point(8, 24)
        Me.lstPlayerSelect.Name = "lstPlayerSelect"
        Me.lstPlayerSelect.Size = New System.Drawing.Size(384, 93)
        Me.lstPlayerSelect.TabIndex = 0
        '
        'frmCampaignerPasswordDialog
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.Black
        Me.ClientSize = New System.Drawing.Size(410, 264)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel1, Me.btnCancel, Me.btnLogon, Me.pnlTopBorder})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.Name = "frmCampaignerPasswordDialog"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.pnlTopBorder.ResumeLayout(False)
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Sub New(ByRef game As cmplib01.CampaignerGameData)
        Me.New()
        ptrGameData = game
        PopulateList()
    End Sub

    Public ReadOnly Property ActionCancelled() As Boolean
        Get
            Return m_ActionCancelled
        End Get
    End Property

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        m_ActionCancelled = True
        Me.Hide()
    End Sub

    Private Sub PopulateList()
        Dim key As String
        Dim play As cmplib01.Player

        For Each key In ptrGameData.Players.Keys
            play = ptrGameData.Players.Item(key)
            If play.TurnComplete = False Or play.PlayerName = "<Moderator>" Then
                lstPlayerSelect.Items.Add(play)
            End If
        Next

    End Sub

    Private Sub btnLogon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogon.Click
        If LogonSuccessful() Then
            ptrGameData.selectedPlayer = lstPlayerSelect.SelectedItem
            ptrGameData.selectedFaction = ptrGameData.Faction_g.Item(ptrGameData.selectedPlayer.FactionKey)
            m_ActionCancelled = False
            Me.Hide()
        End If
    End Sub

    Private Function LogonSuccessful() As Boolean
        If lstPlayerSelect.SelectedItem Is Nothing Then
            MessageBox.Show("Select a player to log in.", "Select Player", MessageBoxButtons.OK)
            Return False
            Exit Function
        End If

        Dim selPlayer As cmplib01.Player = lstPlayerSelect.SelectedItem
        If selPlayer.Password = txtPassword.Text Then
            Return True
        Else
            MessageBox.Show("The password entered for the selected player is not correct.", "Incorrect Password.", MessageBoxButtons.OK)
            Return False
        End If


    End Function

End Class
